package com.simple.datax.transform.impl;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.rpc.RpcException;
import com.simple.datax.SimpleMessage;
import com.simple.datax.api.NeedResendSimpleException;
import com.simple.datax.api.SimpleException;
import com.simple.datax.api.Transformer;
import com.simple.datax.api.TransformerException;
import com.simple.datax.service.AbstractMessageProcessor;
import com.simple.datax.transform.SimpleTransformFactory;
import com.simple.datax.transform.SimpleTransformer;
import com.simple.datax.utils.SimpleConstants;

/**
 * @Title: TransformerImpl.java
 * @Package com.simple.datax.transform.impl
 * @Description: 完成报文转换服务
 * @author Paladin Chen
 * @date 2012-12-31 下午1:22:52
 * @version V1.0
 */
public class TransformerImpl extends AbstractMessageProcessor implements Transformer {
	private static final Logger logger = LoggerFactory.getLogger(TransformerImpl.class);

	private boolean inTransform = true;

	private String localNode = "EC0001";

	private String localEcode = "EC9999999999";

	@Override
	public void realProcess(SimpleMessage src) throws SimpleException {
		logger.debug("Begin to transform: in(" + inTransform + ")" + src.toLog());

		try {
			SimpleTransformer st;
			if (inTransform) {
				setErrorInfos(SimpleConstants.ERROR_TYPE_DATAXPROC, SimpleConstants.ERROR_LEVEL_NORMAL,
						SimpleConstants.RESPONSE_CODE_INMAPPING, SimpleConstants.RESPONSE_MSG_INMAPPING);

				st = SimpleTransformFactory.getInstance().getTransformer(((SimpleMessage) src).getMsgType(),
						((SimpleMessage) src).getMsgBusiType(), ((SimpleMessage) src).isResponse(), inTransform);

				// 处理TCS报文来时的接收者和接收节点
				// 对于TCS的报文，目前都要进本地的FLOW模块
				if ("TCS".equalsIgnoreCase(((SimpleMessage) src).getMsgType())) {
					((SimpleMessage) src).setReceiver(localEcode);
					((SimpleMessage) src).setReceiverNode(localNode);
				}
			} else {
				setErrorInfos(SimpleConstants.ERROR_TYPE_DATAXPROC, SimpleConstants.ERROR_LEVEL_NORMAL,
						SimpleConstants.RESPONSE_CODE_OUTMAPPING, SimpleConstants.RESPONSE_MSG_OUTMAPPING);

				st = SimpleTransformFactory.getInstance().getTransformer(((SimpleMessage) src).getSentMsgType(),
						((SimpleMessage) src).getMsgBusiType(), ((SimpleMessage) src).isResponse(), inTransform);
			}

			st.transform(src);

			this.callNextStep(src);
		} catch (RpcException e) {
			throw new NeedResendSimpleException("Transformer:", e);
		} catch (Exception e) {
			String in = "In";
			if (!inTransform) {
				in = "Out";
			}
			throw new TransformerException(in + " Transform Error:" + e.getMessage(), e);
		}

		logger.debug("Finish transform: in(" + inTransform + ")" + src.toLog());
	}

	public boolean isInTransform() {
		return inTransform;
	}

	// 是否为外部进入时的转换器 true是外部转内部，false为内部转外部
	public void setInTransform(boolean inTransform) {
		this.inTransform = inTransform;
	}

	public String getLocalNode() {
		return localNode;
	}

	public void setLocalNode(String localNode) {
		this.localNode = localNode;
	}

	public String getLocalEcode() {
		return localEcode;
	}

	public void setLocalEcode(String localEcode) {
		this.localEcode = localEcode;
	}

}
